package com.ling.sdk.app

import android.app.Activity
import android.content.Context
import android.os.Bundle
import com.ling.sdk.utils.Toasty
import java.util.*

object ActivityStack : LifecycleCallback() {

    val activities = LinkedList<Activity>()

    fun getTopActivity() = activities.peekLast()


    override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
        activity?.also { activities.add(activity) }
    }

    override fun onActivityDestroyed(activity: Activity?) {
        activity?.also { activities.remove(activity) }
        Toasty.activityDestroy(genSafeKey(activity))
    }

    @JvmStatic
    fun genKey(context: Context?): String? {
        if (context is Activity) return context.javaClass.canonicalName + context.toString()
        return null
    }

    @JvmStatic
    fun genSafeKey(context: Context?): String {
        if (context is Activity) return context.javaClass.canonicalName + context.toString()
        return "Care Leak"
    }


}